package com.wk.springboot.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @User admin
 * @Date 2020/11/26 9:11
 **/
@Component
@Aspect //声明切面类
public class logAspects {

    public logAspects() {
        System.out.println("创建logAspects");
    }

    //提取公用方法
    @Pointcut("execution(public * com.wk.springboot.spring.aop.Calculator.*(..))")
    public void pointCut(){}

    //前置通知
//    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint){
        //获取现在代理的方法是哪个
        System.out.println("joinPoint.getSignature().getName() = " + joinPoint.getSignature().getName());
        //获取代理的类
        System.out.println("joinPoint.getSignature().getDeclaringTypeName() = " + joinPoint.getSignature().getDeclaringTypeName());
        for (int i = 0; i < joinPoint.getArgs().length; i++) {
            System.out.println("joinPoint.getArgs()"+i+":"+joinPoint.getArgs()[i]);
        }
        System.out.println("aop注解开始");
    }

    //后置通知
//    @After("pointCut()")
    public void logEnd(){
        System.out.println("aop注解之后");
    }

    //返回通知
//    @AfterReturning(value = "pointCut()",returning = "result")
    public void logReturn(Object result){
        System.out.println("aop注解正常返回");
        System.out.println("result = " + result);
    }

    //异常通知
    @AfterThrowing(value = "pointCut()",throwing = "ex")
    public void logThrow(Exception ex){
        System.out.println("aop注解抛出异常");
        System.out.println("ex = " + ex);
    }

    //环绕通知。注意要有ProceedingJoinPoint参数传入
//    @Around("pointCut()")
    public void logAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("aop注解环绕开始。。。");
        //执行方法
        pjp.proceed();
        System.out.println("aop环绕注解结束.....");
    }

}
